package com.newsproject.controller;


import com.newsproject.pojo.DTO.PostDTO;
import com.newsproject.pojo.Result;
import com.newsproject.service.PostService;
import com.newsproject.service.UserService;

import io.swagger.annotations.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@Api(tags = "查询文章、微头条、收藏、草稿箱的接口")
public class PageController {
    @Autowired
    private UserService userService;
    @Autowired
    private PostService postService;

    @ApiOperation(value = "查询文章和微头条",notes = "根据文章类型和用户id查询文章,0是查询全部，1是文章，2是微头条，id=0 查自己的")
    @GetMapping("/selectUserPost/{categoryId}")
    @ApiResponses(value = {@ApiResponse(code = 200,
            message = "posts:用户的部分帖子, hasMore ： boolean 是否还有更多 totalCount ：总条数`",
            response =PostDTO.class )})
    @ResponseBody
    public Result<Map<String ,Object>> selectPostByPostTypeAndUserId(
            @ApiParam("文章类型")@PathVariable("categoryId") Integer categoryId,
            @RequestParam(required = false) Integer userId,
            @RequestParam(defaultValue = "1")Integer pageNum,
             HttpSession session){
        return userService.selectPostByPostTypeAndUserId(userId,categoryId,pageNum,session);
    }

    @ApiOperation(value = "查询用户收藏的帖子",notes = "根据用户的id查,id=0,查自己的收藏")
    @GetMapping("/selectPostById/{userId}")
    @ApiResponses(value = {@ApiResponse(code = 200,
            message = "posts:文章，hasMore ： boolean 是否还有更多 totalCount ：总条数",
            response =PostDTO.class )})
    @ResponseBody
    public Result<Map<String ,Object>> getBookMarkPostById(@ApiParam("用户id")@PathVariable("userId") Integer userId,
                                           @RequestParam(defaultValue = "1")Integer pageNum,
                                                           HttpSession session
    ){
        return  userService.getBookMarkPostByUserId(userId,pageNum,session);
    }

    @ApiOperation(value = "查询用户的草稿箱",notes = "根据用户的id查,id=0,查自己的草稿箱")
    @GetMapping("/selectDraft")
    @ApiResponses(value = {@ApiResponse(code = 200,
            message = "posts:文章，hasMore ： boolean 是否还有更多 totalCount ：总条数",
            response =PostDTO.class )})
    @ResponseBody
    public Result<Map<String ,Object>> selectDraftPostByPage(
            @RequestParam(defaultValue = "1")Integer pageNum,
            HttpSession session){
         return userService.selectDraftPostByPage(pageNum,session);
    }

    @ApiOperation(value = "查询用户文章",
            notes = "1.每页10条\n2.不穿userId就是查自己的文章\n" +
                    "3.这几个参数可以组合使用，比如查询自己的文章，并且是审核通过的文章，并且是文字类型的文章\n" +
                    "4.也可以单独使用，比如查询自己的文章，不穿其他参数，就是查询自己的文章\n"
    )
    @GetMapping("/selectUserPost")
    @ApiResponses(value = {@ApiResponse(code = 200,
            message = "posts:文章，hasMore ： boolean 是否还有更多 totalCount ：总条数",
            response =PostDTO.class )})
    @ResponseBody
    public Result<Map<String ,Object>> findPost(
            @ApiParam("页码")@RequestParam( defaultValue = "1")Integer pageNum,
            @ApiParam("用户·id")@RequestParam(required = false) Integer userId,
            @ApiParam("类别id 1：文章 2：微头条") @RequestParam(required = false) Integer categoryId,
            @ApiParam("审核状态 0 未审核 1 通过  2 拒绝") @RequestParam(required = false) Integer auditStatus,
            @ApiParam("是不是草稿 0不是 1 是 ")@RequestParam(required = false) Integer textStatus,
            HttpSession session){
        return postService.findPost(pageNum,userId,categoryId,auditStatus,textStatus,session);
    }
}
